package com.hivision.controller;


import com.hivision.alimqttapi.entity.TechnicalAttribute;
import com.hivision.iot.api.AliReqClient;
import com.hivision.iot.dto.AttributeValueRequestDTO;
import com.hivision.mapper.TechnicalAttributeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@RestController
@Slf4j
public class Jdzt {

    @Autowired
    private AliReqClient reqClient;

    @Autowired
    TechnicalAttributeMapper technicalAttributeMarper;

    private static final Map<String, String> orderMap = new HashMap<>();

    private static final Map<String, String> descriptionMap = new HashMap<>();

    private static final Map<String, String> assetCodeDescriptionMap = new HashMap<>();

    //DN_PS	涂装车间
    // DN_PTED	前处理电泳
    // DN_PVC1	PVC1
    // DN_TC1	面漆1
    // DN_TC2	面漆2
    // DN_WS	车间
    // DN_Public	公用
    // DN_Con	机运
    private static final List<String> list = Arrays.asList("DN_PS", "DN_PTED", "DN_PVC1", "DN_TC1", "DN_TC2", "DN_WS", "DN_Public","DN_Con");

    static {

        orderMap.put("OpenLine", "TemRise");
        orderMap.put("up", "Produce");
        orderMap.put("steady", "Shutdown");
        orderMap.put("shutdown", "LineStop");
        orderMap.put("LineStop", "OpenLine");


        descriptionMap.put("OpenLine", "开机");
        descriptionMap.put("TemRise", "升温");
        descriptionMap.put("Produce", "稳态");
        descriptionMap.put("Shutdown", "关机");
        descriptionMap.put("LineStop", "关机完成");

        assetCodeDescriptionMap.put("DN_PS","涂装车间");
        assetCodeDescriptionMap.put("DN_PTED","前处理电泳");
        assetCodeDescriptionMap.put("DN_PVC1","PVC1");
        assetCodeDescriptionMap.put("DN_TC1","面漆1");
        assetCodeDescriptionMap.put("DN_TC2","面漆2");
        assetCodeDescriptionMap.put("DN_WS","车间");
        assetCodeDescriptionMap.put("DN_Public","公用");
        assetCodeDescriptionMap.put("DN_Con","机运");

    }

    @Scheduled(cron = "0 */1 * * * ?")
    public void writeValue() {

        // 获取当前系统的日期时间，考虑到时区
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
        // 创建一个日期时间格式器，匹配所需的格式
        DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
        // 格式化当前日期时间为字符串
        String formattedNow = now.format(formatter);


        TechnicalAttribute query = new TechnicalAttribute();

        for (String assetCode : list) {

            query.setAttributeGroupName(assetCode);
            TechnicalAttribute result = technicalAttributeMarper.selectTechnicalAttributeByCondition(query);

            AttributeValueRequestDTO attributeValueRequestDTO = new AttributeValueRequestDTO();

            if (result == null) {
                //首次添加数据
                attributeValueRequestDTO.setAssetCode(assetCode);
                attributeValueRequestDTO.setAttributeCode("OpenLine");
                attributeValueRequestDTO.setValue("1");
                reqClient.patchAttributeValue(attributeValueRequestDTO);

                result = new TechnicalAttribute();
                result.setWritable("t");
                result.setMeasureUni("无");
                result.setMinValue("min无");
                result.setMaxValueNew("max无");
                result.setAssetCode(assetCode);
                result.setAttributeName(assetCodeDescriptionMap.get(assetCode));
                result.setDataTypeNew("Integer");
                result.setAttributeGroupName(assetCode);
                result.setAttributeCode("OpenLine");
                result.setAttributeDescs("开机");
                result.setAttributeGroupDescs("无");
                result.setEnumValue("1");
                result.setPrecisionNew(36L);
                result.setCreatedAt(formattedNow);
                result.setModifiedAt(formattedNow);
                technicalAttributeMarper.insertTechnicalAttribute(result);

            } else {
                String old = result.getAttributeCode();
                String next = orderMap.get(old);

                //更新上一执行步骤已结束状态  为0
                attributeValueRequestDTO.setAssetCode(assetCode);
                attributeValueRequestDTO.setAttributeCode(old);
                attributeValueRequestDTO.setValue("0");
                reqClient.patchAttributeValue(attributeValueRequestDTO);
                //更新下一执行步骤开始状态  为1
                attributeValueRequestDTO.setAttributeCode(next);
                attributeValueRequestDTO.setValue("1");
                reqClient.patchAttributeValue(attributeValueRequestDTO);

                //更新之前状态
                result.setEnumValue("0");
                result.setModifiedAt(formattedNow);
                technicalAttributeMarper.updateTechnicalAttribute(result);

                //存储新状态
                result.setAttributeCode(next);
                result.setAttributeDescs(descriptionMap.get(next));
                result.setEnumValue("1");
                result.setCreatedAt(formattedNow);
                result.setModifiedAt(formattedNow);
                technicalAttributeMarper.insertTechnicalAttribute(result);
            }
            log.info("值为" + attributeValueRequestDTO);
        }

    }

}
